﻿using B.S.RbacData.Domain.Traceabilitys;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Read.API.Applications.Commands.Traceabilitys;
using B.S.RbacData.Read.API.Applications.Commands.Traceabilitys.Farmings;
using B.S.RbacData.Read.API.Applications.Commands.Traceabilitys.Slaughters;
using B.S.RbacData.Read.API.Applications.Commands.Traceabilitys.DeepProcessings;
using B.S.RbacData.Read.API.Dtos.Traceabilitys;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.RbacData.Read.API.Controllers
{
    /// <summary>
    /// 溯源管理（读）
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TraceabilityMenganmentController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<TraceabilityMenganmentController> logger;
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="mediator">中介者</param>

        public TraceabilityMenganmentController(ILogger<TraceabilityMenganmentController> logger,IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
        }

        /// <summary>
        /// 获取产品信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<ApiPaging<GetProductDtos>> GetProduct([FromQuery]GetProductCommand command)
        {

            try
            {
               return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("获取产品信息异常处理" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 获取所有的商品信息
        /// </summary>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<ApiResult<List<GetAllGoodsDtos>>> GetGoods()
        {

            try
            {
                GetAllGoodsCommand command = new GetAllGoodsCommand();
                return await mediator.Send(command);

            }
            catch (Exception ex)
            {
                logger.LogError("获取商品信息异常处理" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 产品详情反填
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<ApiResult<GetEnditDtos>> GetProductEndit([FromQuery]GetEnditCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("获取产品详情信息异常处理" + ex.Message);
                throw;
            }

        }

        /// <summary>
        /// 获取图片信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<ApiResult<List<ImagesModel>>> GetImgs([FromQuery]GetAllImgsCommand command)
        {

            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("获取图片信息异常处理" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 获取养殖信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<ApiResult<List<FarmingModel>>> GetFarming([FromQuery]GetFarmingCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("获取养殖信息异常处理" + ex.Message);
                throw;
            }
        }


        /// <summary>
        /// 获取养殖保健信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<ApiResult<List<FarmingHealthcareModel>>> GetFarmingHealth([FromQuery]GetFarmingHealthCommand command)
        {

            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("获取养殖保健信息异常处理" + ex.Message);
                throw;
            }
        }

       /// <summary>
       /// 获取养殖企业信息
       /// </summary>
       /// <param name="command">命令</param>
       /// <returns>返回任务</returns>
        [HttpGet]

        public  async Task<ApiResult<List<FarmingCorporateModel>>> GetFarmingCorporate([FromQuery]GetFarmingCorporateCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("获取养殖企业信息异常处理" + ex.Message);
                throw;
            }
        }


        /// <summary>
        /// 获取屠宰信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpGet]

        public async Task<ApiResult<List<SlaughterModel>>> GetSlaughter([FromQuery] GetSlaughterCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("获取屠宰信息异常处理" + ex.Message);
                throw;
            }
        }


        

        /// <summary>
        /// 获取屠宰企业信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpGet]

        public async Task<ApiResult<List<SlaughterCorporateModel>>> GetSlaughterCorporate([FromQuery] GetSlaughterCorporateCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("获取屠宰企业信息异常处理" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 获取深加工信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<ApiResult<List<DeepProcessingModel>>> GetDeepProcessing([FromQuery] GetDeepProcessingCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("获取深加工信息异常处理" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 获取深加工企业信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<ApiResult<List<DeepProcessingCorporateModel>>> GetDeepProcessingCorporate([FromQuery] GetDeepProcessingCorporateCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("获取深加工企业信息异常处理" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 获取供应商信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<ApiResult<List<SupplierModel>>> GetSupplier([FromQuery] GetSupplierCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("获取供应商信息异常处理" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 根据ID获取产品信息
        /// </summary>
        /// <param name="id">产品ID</param>
        /// <returns>返回产品信息</returns>
        [HttpGet]
        public async Task<ApiResult<GetProductDtos>> GetProductById([FromQuery] int id)
        {
            try
            {
                var command = new GetProductByIdCommand { Id = id };
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("根据ID获取产品信息异常处理" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 获取溯源详情
        /// </summary>
        /// <param name="qrCodeId">溯源码ID</param>
        /// <returns>返回溯源详情</returns>
        [HttpGet]
        public async Task<ApiResult<object>> GetTraceabilityDetail([FromQuery] string qrCodeId)
        {
            try
            {
                var command = new GetTraceabilityDetailCommand { QrCodeId = qrCodeId };
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("获取溯源详情异常处理: " + ex.Message);
                return new ApiResult<object>
                {
                    code = ResultEnumCode.fails,
                    message = "获取溯源详情失败: " + ex.Message,
                    data = null
                };
            }
        }
    }
}
